package cn.ante.ymcc.web.controller;

import cn.ante.ymcc.dto.CourseAddDto;
import cn.ante.ymcc.service.ICourseService;
import cn.ante.ymcc.domain.Course;
import cn.ante.ymcc.query.CourseQuery;
import cn.ante.ymcc.result.JSONResult;
import cn.ante.ymcc.result.PageList;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Wrapper;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
    * 课程新增
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
//    @PreAuthorize("hasAuthority('course:save')")
    public JSONResult save(@RequestBody @Valid CourseAddDto courseAddDto){
        return courseService.save(courseAddDto);
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }

    /**
     * 查询所有对象
     */
    @PreAuthorize("hasAuthority('course:list')")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }



    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page, entityWrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }

    /**
    * @Title: 课程发布上架
    * @Description:
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/8/14 15:39
    * @Parameters:
    * @Return
    */
    @RequestMapping(value="/onLineCourse/{id}",method= RequestMethod.POST)
    @PreAuthorize("hasAuthority('course:online')")
    public JSONResult onLineCourse(@PathVariable("id") Long courseId){
        return courseService.onLineCourse(courseId);
    }


   /** 
   * @Title: 课程下架，并删除es中的记录
   * @Description: TODO
   * @Author: Nai
   * @Version: 1.0
   * @Date:  2022/8/14 18:48  
   * @Parameters: [courseId]
   * @Return cn.ante.ymcc.result.JSONResult
   */
    @RequestMapping(value="/offLineCourse/{id}",method= RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("id") Long courseId){
        return courseService.offLineCourse(courseId);
    }


    /**
    * @Description: 课程详情页展示
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/8/17 11:16
    */
    @RequestMapping(value="/detail/data/{courseId}",method= RequestMethod.GET)
    public JSONResult getCourseDetail(@PathVariable("courseId") Long courseId){
        return JSONResult.success(courseService.getCourseDetail(courseId));
    }


    /**
     * @Description: 课程加入购物车显示结算订单
     * @Author: Nai
     * @Version: 1.0
     * @Date:  2022/8/17 11:16
     */
    @RequestMapping(value="/info/{courseIds}",method= RequestMethod.GET)
    public JSONResult courseOrderInfo(@PathVariable("courseIds") String courseIds){
        return JSONResult.success(courseService.courseOrderInfo(courseIds));
    }
}
